//index.js
//获取应用实例
const app = getApp()
console.log(app.globalData)

Page({
  data: {
    seach_value:''
  },
  onGotUserInfo: function (e) {
    let that = this;
    if(!app.globalData.opid == ""){
      wx.showLoading({
        title: '马上投票',
      })
      wx.request({
        url: app.globalData.url + 'voting.php?Action=toupiao',
        data: {
          openid: app.globalData.opid,
          id: e.currentTarget.dataset.id
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading()
          if (res.data.success == 1) {
            wx.showToast({
              title: res.data.msg,
              duration: 1000,
              mask: true,
              icon: 'success',
            })
           setTimeout(function(){
             wx.showModal({
               title: '温馨提醒',
               content: '您获得一次抽奖机会，是否进行抽奖？',
               success(res) {
                 if (res.confirm) {
                   wx.navigateTo({
                     url: '../luck_draw/luck_draw',
                   })
                 } else if (res.cancel) {
                   console.log('用户点击取消')
                 }
               }
             })
           },2000)
            // 统计
            wx.request({
              url: app.globalData.url + 'voting.php?Action=tongji',
              success: function (res) {
                console.log(res)
                that.setData({
                  tongji: res.data
                })
              }
            })
            wx.request({
              url: app.globalData.url + 'voting.php?Action=votelist',
              success: function (res) {
                console.log(res)
                that.setData({
                  votelist: res.data
                })
              }
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              duration: 1000,
              mask: true,
              icon: 'none',
            })
          }

        }
      })
      return false
    }
    if (e.detail.userInfo) {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      app.globalData.loginState = false
      that.setData({
        loginState: false
      })
      wx.request({
        url: app.globalData.url + 'wxlogin.php',
        data: {
          name:
            JSON.stringify({
              code: app.globalData.code,
              user: e.detail.userInfo,
              openid: app.globalData.shareopenid
            })
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (loginOk) {
          console.log(loginOk.data)
            wx.setStorageSync('opid', loginOk.data.openid)
            app.globalData.opid = loginOk.data.openid;
          app.globalData.userInfo = e.detail.userInfo;
          wx.hideLoading()
          if (loginOk.data.openid) {
            wx.request({
              url: app.globalData.url + 'voting.php?Action=toupiao',
              data: {
                openid: app.globalData.opid,
                id: e.currentTarget.dataset.id
              },
              success: function (res) {
                console.log(res)
                wx.hideLoading()
                if (res.data.success == 1) {
                  wx.showToast({
                    title: res.data.msg,
                    duration: 1000,
                    mask: true,
                    icon: 'success',
                  })
                  setTimeout(function () {
                    wx.showModal({
                      title: '温馨提醒',
                      content: '您获得一次抽奖机会，是否进行抽奖？',
                      success(res) {
                        if (res.confirm) {
                          wx.navigateTo({
                            url: '../luck_draw/luck_draw',
                          })
                        } else if (res.cancel) {
                          console.log('用户点击取消')
                        }
                      }
                    })
                  }, 1000)
                  // 统计
                  wx.request({
                    url: app.globalData.url + 'voting.php?Action=tongji',
                    success: function (res) {
                      console.log(res)
                      that.setData({
                        tongji: res.data
                      })
                    }
                  })
                  wx.request({
                    url: app.globalData.url + 'voting.php?Action=votelist',
                    success: function (res) {
                      console.log(res)
                      that.setData({
                        votelist: res.data
                      })
                    }
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    duration: 1000,
                    mask: true,
                    icon: 'none',
                  })
                }

              }
            })
          }

          
        }
      })
    }
    else {
      wx.showModal({
        title: '用户信息授权失败，请重新授权',
        content: '为了更好的为你提供服务，我们申请获得你的公开用户信息（昵称，头像）',
        showCancel: false,
        confirmText: '去授权',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
                if (res.authSetting["scope.userInfo"]) {
                  app.globalData.loginState = false
                  app.globalData.loginFn(e.detail.userInfo)
                  wx.getSetting({
                    success(res) {
                      res.authSetting['scope.userInfo'] = false
                    }
                  })

                }
              }

            })
          }
        }
      })
    }
  },
  go_detail:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },
  go_url:function(e){
    console.log(e.target.dataset.url)
    wx.navigateTo({
      url: e.target.dataset.url
    })
    wx.switchTab({
      url: e.currentTarget.dataset.url,
    })
  },
  // 搜索
  changeinput:function(e){
    var that = this;
    that.setData({
      seach_value:e.detail.value
    })
  },
  seach:function(){
    var that = this;
    
    // 搜索接口
    wx.request({
      url: app.globalData.url + 'voting.php?Action=votelist',
      data: {
        keywords: that.data.seach_value
      },
      success: function (res) {
        console.log(res)
        that.setData({
          votelist: res.data
        })

      }
    })
  },
  // 投票
  toupiao:function(e){
    
  },
  // 轮播图点击
  go_lunbo:function(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
    wx.switchTab({
      url: e.currentTarget.dataset.url,
    })
  },
  onLoad: function (options) {
   
    if(options.id){
      app.globalData.shareopenid = options.id;
      console.log(options.id)
      
    }
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    // 投票列表
    wx.request({
      url: app.globalData.url +'voting.php?Action=votelist',
      success:function(res){
        console.log(res)
        that.setData({
          votelist:res.data
        })
      }
    })
    // 统计
    wx.request({
      url: app.globalData.url +'voting.php?Action=tongji',
      success:function(res){
        console.log(res)
        that.setData({
          tongji:res.data
        })
      }
    })
    // 轮播图
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=touch_ad',
      success:function(res){
        console.log(res)
        that.setData({
          lunbo:res.data
        })
        wx.hideLoading()
      }
    })
    
  },
  onShow:function(){
    var that = this;
    // 投票列表
    wx.request({
      url: app.globalData.url + 'voting.php?Action=votelist',
      success: function (res) {
        console.log(res)
        that.setData({
          votelist: res.data
        })
      }
    })
    // 统计
    wx.request({
      url: app.globalData.url + 'voting.php?Action=tongji',
      success: function (res) {
        console.log(res)
        that.setData({
          tongji: res.data
        })
      }
    })
    // 轮播图
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=touch_ad',
      success: function (res) {
        console.log(res)
        that.setData({
          lunbo: res.data
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  
})
